package com.zr.sms.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.sms.model.Employee;
import com.zr.sms.service.AddService;
import com.zr.sms.serviceimpl.AddServiceImpl;

import net.sf.json.JSONObject;

public class SubmitdinghuoorderServlet extends HttpServlet{
	AddService add=new AddServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		 doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		 req.setCharacterEncoding("utf-8");
		 resp.setCharacterEncoding("utf-8");
		 String dinghuoorder=req.getParameter("dinghuoorder");
		 JSONObject jobj=JSONObject.fromObject(dinghuoorder);
		 String gnumber=jobj.getString("gnumber");
		 String[] gnumbers=gnumber.split(",");
		 String gcost=jobj.getString("gcost");
		 String[]gcosts=gcost.split(",");
		 double totalmoney=0;//获取订单总金额
		 for (int i = 0; i < gcosts.length; i++) {
			totalmoney=totalmoney+Integer.parseInt(gnumbers[i])*Double.parseDouble(gcosts[i]);
		}
		 Employee emp=(Employee) req.getSession().getAttribute("currentemp");
		 int eid=emp.getEid();//获取当前订货员id
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date date=new Date();
		 String dateStr=sdf.format(date);//获取当前时间
		 boolean flag=add.addGoodsList(dateStr, jobj.toString(), totalmoney, eid);
		 JSONObject json=new JSONObject();
		 json.put("msg", "订单生成成功");
		 json.put("totalmoney", totalmoney);
		 PrintWriter out=resp.getWriter();
		 out.write(json.toString());
		 
		 
		 
		 
		 
	}

}
